import socket
from threading import Thread
from SocketServer import ThreadingMixIn
from socket import error as SocketError
import errno

# Multithreaded Python server : TCP Server Socket Thread Pool
class ClientThread(Thread):

    def __init__(self, conn):
        Thread.__init__(self)
        self.conn = conn
        print "[+] New server socket thread started for " + ip + ":" + str(port)

    def run(self):
        while True:
            try:
                data = self.conn.recv(10240)
            except SocketError as e:
                print e.errno


# Multithreaded Python server : TCP Server Socket Program Stub
TCP_IP = '0.0.0.0'
TCP_PORT = 8890

tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024000)
# tcpServer.settimeout(5)
tcpServer.bind((TCP_IP, TCP_PORT))
threads = []

while True:
    tcpServer.listen(10000)
    # print "Multithreaded Python server : Waiting for connections from TCP clients..."
    (conn, (ip, port)) = tcpServer.accept()
    newthread = ClientThread(conn)
    newthread.start()
    threads.append(newthread)